package com.example.event_resource_java.authority.controller.ClassController;

import com.example.event_resource_java.authority.server.ClassService;
import com.example.event_resource_java.commons.util.ApiResult;
import com.example.event_resource_java.commons.vo.AddClassVO;
import com.example.event_resource_java.commons.vo.ResetVO;
import com.example.event_resource_java.commons.vo.ClassVO.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Base64;

/**
 * @ClassName ClassController
 * @Description: 班级管理接口
 * @Author: ak
 * @CreateDate: 2024/4/15 13:38
 * @UpdateUser:
 * @UpdateDate: 2024/4/15 13:38
 * @UpdateRemark:
 * @Version: 1.0
 */

@RestController
@RequestMapping("/class")
@CrossOrigin
public class ClassController {

    @Autowired
    private ClassService classService;

    /**
     * @auther: ak
     * @Description 查询班级
     * @param:
     * @return:
     * @date: 2024/4/16 13:46
     */
    @GetMapping("/getClass")
    public ApiResult list(@RequestParam("className") String className,
                                @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        return ApiResult.success(classService.list(className, pageNum, pageSize));
    }

    /**
     * @auther: ak
     * @Description 新增班级 班级名称  批量添加班级学生
     * @param:
     * @return:
     * @date: 2024/4/16 13:44
     */
    @PostMapping("/save")
    public ApiResult save(@RequestBody AddClassVO addClassVO) {
        ApiResult apiResult = classService.batchSaveClass(addClassVO.getClassname(), addClassVO.getStudentVOList());
        return apiResult;
    }

    /**
     * @auther: ak
     * @Description 查询班级详情 分页查询 姓名或用户名查询学生 或 全部
     * @param:
     * @return:
     * @date: 2024/4/16 13:45
     */
    @GetMapping("/getClassDetails")
    public ApiResult getClassDetails(@RequestParam("name") String name, @RequestParam("mobile") String mobile,
                                     @RequestParam("classId") Long classId, @RequestParam("pageNum") Integer pageNum,
                                     @RequestParam("pageSize") Integer pageSize) {
        return ApiResult.success(classService.getClassDetails(name,
                mobile, classId, pageNum, pageSize));
    }

    /**
     * @auther: ak
     * @Description 修改学生用户信息
     * @param:
     * @return:
     * @date: 2024/4/17 16:06
     */
    @PostMapping("/updateStu")
    public ApiResult updateStu(@RequestBody StudentVO studentVO) {
        boolean b = classService.updateStu(studentVO.getName(), studentVO.getMobile(),
                studentVO.getSex(), studentVO.getId(), null);
        return ApiResult.success(b);
    }

    /**
     * @auther: ak
     * @Description 退出班级
     * @param:
     * @return:
     * @date: 2024/4/17 16:07
     */
    @GetMapping("/quitClass")
    public ApiResult quitStu(@RequestParam("userId") Long userId, @RequestParam("classId") Long classId) {
        return ApiResult.success(classService.quitStu(userId, classId));
    }

    /**
     * @auther: ak
     * @Description 重置学生密码
     * @param:
     * @return:
     * @date: 2024/4/17 16:10
     */
    @PostMapping("/resetPwd")
    public ApiResult resetPwd(@RequestBody ResetVO resetVO) {
        boolean b = classService.updateStu(null, null,
                null, resetVO.getId(), resetVO.getPassword());
        return ApiResult.success(b);
    }

    /**
     * @auther: ak
     * @Description 修改班级信息
     * @param:
     * @return:
     * @date: 2024/4/17 16:14
     */
    @GetMapping("/updateClass")
    public ApiResult updateClass(@RequestParam("classId") Long classId, @RequestParam("className") String classname) {
        return ApiResult.success(classService.updateClass(classId, classname));
    }

    /**
     * @auther: ak
     * @Description 判断班级是否存在
     * @param:
     * @return:
     * @date: 2024/4/16 13:46
     */
    @GetMapping("/isExistClass")
    public ApiResult isExistClass(@RequestParam("className") String className) {
        return ApiResult.success(classService.isExistClass(className));
    }

    /**
     * @auther: ak
     * @Description 获取老师班级列表
     * @param:
     * @return:
     * @date: 2024/5/9 14:48
     */
    @GetMapping("/classList")
    public ApiResult classList() {
        return ApiResult.success(classService.classList());
    }

    /**
     * @auther: ak
     * @Description 删除班级
     * @return:
     * @date: 2024/5/9 14:48
     */
    @GetMapping("/delete")
    public ApiResult deleteClass(@RequestParam("classId") String classId) {
        return ApiResult.success(classService.deleteClass(Long.parseLong(classId)));
    }

}
